using System;
using System.IO;

namespace WeifenLuo.WinFormsUI.Base
{
	/// <summary>
	/// Summary description for Ctrl_LOG.
	/// </summary>
	public class Ctrl_LOG
	{
		static bool initialized = false;
		static BinaryWriter logfile = null;
		static string	ms_path;

		static public void SetPath(string as_path)
		{
			ms_path = as_path;
		}

		static private bool initialize()
		{
			if (initialized)
			{
				return (logfile != null);
			}
			else
			{
				initialized = true;
				string pathFile = (ms_path == null || ms_path =="")?Environment.CurrentDirectory + @"\controllers.log":ms_path + @"\3S-PAYROLL.log";
				if (pathFile == null) return false;
				try
				{
					FileStream s = new FileStream(pathFile, FileMode.Append, FileAccess.Write);
					logfile = new BinaryWriter(s);
					string buf = "======== Logging started at " + System.DateTime.Now.ToString() + " ========\r\n";
					logfile.Write(buf.ToCharArray(), 0, buf.Length);
					logfile.Flush();
					return true;
				}
				catch (Exception)
				{
					logfile = null;
					return false;
				}
			}
		}

		static public void Print(string text)
		{
			if (!initialize()) return;
		
			string buf = text + "\r\n";
			logfile.Write(buf.ToCharArray(), 0, buf.Length);
			logfile.Flush();
		}

		static public void Print(string text, string a)
		{
			if (!initialize()) return;

			string buf = text + "\r\n  " + a + "\r\n";
			logfile.Write(buf.ToCharArray(), 0, buf.Length);
			logfile.Flush();
		}

		static public void Print(string text, string a, string b)
		{
			if (!initialize()) return;
			string buf = text + "\r\n  " + a + "\r\n  " + b + "\r\n";
			logfile.Write(buf.ToCharArray(), 0, buf.Length);
			logfile.Flush();
		}

		static public void Print(string text, string a, string b, string c)
		{
			if (!initialize()) return;

			string buf = text + "\r\n  " + a + "\r\n  " + b + "\r\n  " + c + "\r\n";
			logfile.Write(buf.ToCharArray(), 0, buf.Length);
			logfile.Flush();
		}


		static public void Print(Exception exception)
		{
			if (!initialize()) return;

			string buf = exception.ToString();
			logfile.Write(buf.ToCharArray(), 0, buf.Length);
			logfile.Write("\n\r");
			logfile.Flush();
		}
	}
}
